package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理等级Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private static final String NURSING_LEVEL_KEY = "nursingLevel:all";

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel){
        //新增数据库
        int insert = nursingLevelMapper.insert(nursingLevel);

        //清理缓存
        redisTemplate.delete(NURSING_LEVEL_KEY);
        return insert;

    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        //修改数据库
        int i =  nursingLevelMapper.updateById(nursingLevel);

        //清理缓存
        redisTemplate.delete(NURSING_LEVEL_KEY);
        return i;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        //删除数据库
        int i = nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
        //清理缓存
        redisTemplate.delete(NURSING_LEVEL_KEY);
        return i;
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        //删除数据库
        int i =nursingLevelMapper.deleteById(id);

        //清理缓存
        redisTemplate.delete(NURSING_LEVEL_KEY);
        return i;
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }

    /**
     * 获取所有护理等级
     *
     * @return 结果
     */
    @Override
    public List<NursingLevel> all() {
        //1. 从缓存中获取
        List<NursingLevel> list = (List<NursingLevel>) redisTemplate.opsForValue().get("NURSING_LEVEL_KEY");

        //2. 如果缓存命中，则返回
        if (CollUtil.isNotEmpty( list)){
            return list;
        }

        //3.没有命中，则查询数据库
        list = lambdaQuery().eq(NursingLevel::getStatus, 1).list();

        //4. 缓存结果,过期时间24小时
        redisTemplate.opsForValue().set(NURSING_LEVEL_KEY, list, 24 * 60 * 60);
        return list;
    }
}
